// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   XlsWriter.java

package include.excel_export;

import include.nseer_db.nseer_db;
import java.io.FileOutputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import org.apache.poi.hssf.usermodel.*;

// Referenced classes of package include.excel_export:
//			DataBaseInfo, Masking

public class XlsWriter
{

	Masking reader;
	String sql;
	DataBaseInfo info;
	String configFile;
	nseer_db db;
	HSSFCellStyle doubleCellStyle;
	HSSFCellStyle intCellStyle;
	HSSFCellStyle dateCellStyle;

	public XlsWriter(String database, String configFile)
	{
		init(database, configFile);
	}

	public XlsWriter()
	{
	}

	public void setSqlCondition(String condition)
	{
		sql = condition;
	}

	public void setDatabase(String database)
	{
		init(database, null);
	}

	public void setConfigFile(String configFile)
	{
		this.configFile = configFile;
	}

	private void init(String database, String configfile)
	{
		info = new DataBaseInfo(database);
		db = new nseer_db(database);
		configFile = configfile;
	}

	private void initCellTyles(HSSFWorkbook wb)
	{
		HSSFDataFormat df = wb.createDataFormat();
		short doubleFormat = df.getFormat("#,##0.00");
		short intFormat = df.getFormat("0");
		short dateFormat = df.getFormat("yyyy-mm-dd");
		doubleCellStyle = wb.createCellStyle();
		doubleCellStyle.setDataFormat(doubleFormat);
		intCellStyle = wb.createCellStyle();
		intCellStyle.setDataFormat(intFormat);
		dateCellStyle = wb.createCellStyle();
		dateCellStyle.setDataFormat(dateFormat);
	}

	public void write(String filename)
	{
		try
		{
			FileOutputStream fos = new FileOutputStream(filename);
			HSSFWorkbook wb = new HSSFWorkbook();
			initCellTyles(wb);
			reader = new Masking(configFile);
			Vector tables = reader.getTableNicks();
			Iterator loop = tables.iterator();
			int i = 0;
			while (loop.hasNext()) 
			{
				HSSFSheet sheet = wb.createSheet();
				String tablenick = (String)loop.next();
				wb.setSheetName(i, tablenick, (short)1);
				Vector columns = reader.getColumnNicks(tablenick);
				writeTitle(columns, sheet);
				Vector columnNames = reader.getColumnNames(tablenick);
				writeContent(columnNames, sheet, reader.getTableNameFormNick(tablenick));
				i++;
				sheet = null;
				tablenick = null;
				columns = null;
				columnNames = null;
			}
			wb.write(fos);
			fos.close();
			break MISSING_BLOCK_LABEL_211;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		db.close();
		info.finalize();
		break MISSING_BLOCK_LABEL_225;
		Exception exception;
		exception;
		db.close();
		info.finalize();
		throw exception;
		db.close();
		info.finalize();
	}

	public void writeContent(Collection columns, HSSFSheet sheet, String table)
		throws Exception
	{
		int i = 1;
		Object names[] = new Object[columns.size()];
		columns.toArray(names);
		info.setTable(table);
		String SQL;
		if (sql == null)
			SQL = (new StringBuilder("select * from ")).append(table).toString();
		else
			SQL = (new StringBuilder("select * from ")).append(table).append(" ").append(sql).toString();
		ResultSet rs = db.executeQuery(SQL);
		try
		{
			while (rs.next()) 
			{
				HSSFRow row = sheet.createRow((short)i);
				for (int j = 0; j < columns.size(); j++)
				{
					HSSFCell topcell = row.createCell((short)j);
					String column = (String)names[j];
					topcell.setEncoding((short)1);
					switch (getType(column))
					{
					case 0: // '\0'
						topcell.setCellType(0);
						topcell.setCellStyle(doubleCellStyle);
						topcell.setCellValue(rs.getDouble(column));
						break;

					case 1: // '\001'
						topcell.setCellType(1);
						topcell.setCellValue(rs.getString(column));
						break;

					case 200: 
						topcell.setCellType(0);
						topcell.setCellStyle(intCellStyle);
						topcell.setCellValue(rs.getInt(column));
						break;

					case 300: 
						topcell.setCellType(1);
						topcell.setCellStyle(dateCellStyle);
						topcell.setCellValue(rs.getString(column));
						break;

					default:
						topcell.setCellType(1);
						topcell.setCellValue(rs.getString(column));
						break;
					}
					topcell = null;
					column = null;
				}

				i++;
				row = null;
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
	}

	public void writeTitle(Collection columns, HSSFSheet sheet)
	{
		Object names[] = new Object[columns.size()];
		columns.toArray(names);
		HSSFRow row = sheet.createRow(0);
		for (int i = 0; i < columns.size(); i++)
		{
			HSSFCell cell = row.createCell((short)i);
			String column = (String)names[i];
			cell.setEncoding((short)1);
			cell.setCellType(1);
			cell.setCellValue(column);
			cell = null;
			column = null;
		}

		row = null;
		names = (Object[])null;
	}

	public int getType(String column)
		throws Exception
	{
		String type = info.getColumnType(column);
		int hssfType;
		if (type != null)
		{
			if (type.equals("DOUBLE") || type.equals("DOUBLE PRECISION") || type.equals("FLOAT"))
				hssfType = 0;
			else
			if (type.equals("INTEGER") || type.equals("TINY") || type.equals("SHORT") || type.equals("LONG"))
				hssfType = 200;
			else
			if (type.equals("DATE") || type.equals("TIMESTAMP"))
				hssfType = 300;
			else
				hssfType = 1;
		} else
		{
			hssfType = 1;
		}
		return hssfType;
	}

	public void setCondition(String sql)
	{
		this.sql = sql;
	}

	public static void main(String args[])
	{
		XlsWriter mission = new XlsWriter("crm", "table.xml");
		mission.write("Eoo.xls");
	}
}
